package com.intelligent.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.intelligent.pojo.BomInfo;
import com.intelligent.service.BomInfoService;
import com.intelligent.mapper.BomInfoMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
* @author lenovo
* @description 针对表【bom_info(BOM信息表)】的数据库操作Service实现
* @createDate 2025-08-28 09:01:55
*/
@Service
public class BomInfoServiceImpl extends ServiceImpl<BomInfoMapper, BomInfo>
    implements BomInfoService{

    @Override
    public BomInfo getBomInfoById(Long bomId) {
        return baseMapper.getBomInfoById(bomId);
    }

    @Override
    public IPage<BomInfo> pageBomInfo(Page<BomInfo> page, String bomCode, String bomName,
                                      String productCode, String productName, Integer status) {
        return baseMapper.pageBomInfo(page, bomCode, bomName, productCode, productName, status);
    }

    @Override
    public List<BomInfo> exportBomInfoToExcel(String bomCode, String bomName,
                                              String productCode, String productName, Integer status) {
        return baseMapper.exportBomInfoToExcel(bomCode, bomName, productCode, productName, status);
    }

    @Override
    public boolean addBomInfo(BomInfo bomInfo) {
        // 自动生成BOM编号
        String bomCode = generateBomCode();
        bomInfo.setBomCode(bomCode);

        // 设置默认状态为启用（0）
        if (bomInfo.getStatus() == null) {
            bomInfo.setStatus(0);
        }

        // 插入数据
        int result = baseMapper.insertBomInfo(bomInfo);
        return result > 0;
    }

    /**
     * 自动生成BOM编号
     * 格式：BOM + 年月日 + 4位序号，例如：BOM202501010001
     *
     * @return 生成的BOM编号
     */
    private String generateBomCode() {
        // 获取当前日期
        String currentDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));

        // 获取当前日期下的最大编号
        String maxBomCode = baseMapper.getMaxBomCode();

        int sequence = 1;
        if (maxBomCode != null && maxBomCode.startsWith("BOM" + currentDate)) {
            // 提取序号部分
            String sequenceStr = maxBomCode.substring(11); // BOM20250101 + 4位序号
            try {
                sequence = Integer.parseInt(sequenceStr) + 1;
            } catch (NumberFormatException e) {
                sequence = 1;
            }
        }

        // 格式化序号为4位数字
        String formattedSequence = String.format("%04d", sequence);

        return "BOM" + currentDate + formattedSequence;
    }

    @Override
    public boolean updateBomInfo(BomInfo bomInfo) {
        // 检查BOM信息是否存在
        if (bomInfo.getBomId() == null) {
            return false;
        }

        // 更新数据
        int result = baseMapper.updateBomInfo(bomInfo);
        return result > 0;
    }

    @Override
    public boolean deleteBomInfo(Long bomId) {
        // 检查BOM ID是否有效
        if (bomId == null) {
            return false;
        }

        // 删除数据
        int result = baseMapper.deleteByBomId(bomId);
        return result > 0;
    }
}




